

function netadmin.Initialize() --This function is called when the server 1st loads the gamemode. For right now all this does is start the connection and load the config to ulx.
	--Checking to make sure GDatabase is installed
	if !GDATABASE_INSTALLED then
		Msg("NETADMIN: GDatabase is not installed, please install GDatabase V1.0 or above\n")
		return
	end
	
	--Connection section
	netadmin.Connect()
	if !gdatabase.CheckForConnection(netadmin.DB) then 
		timer.Create( "netadminreconnecter", 3600, 0, netadmin.Initialize)
		ptimer.Simple(.2, netadmin.LoadBackupConfig)
		return 
	else
		if timer.IsTimer("netadminreconnecter") then
			timer.Destroy("netadminreconnecter")
		end
	end
	
	--Process all the configs

	ptimer.Simple(.2, netadmin.ProcessConfigs) --Setup a timer so it loads the config .2 seconds after this line so that the server has a chance to load everything.
end

hook.Add("Initialize", "netadminInitialize", netadmin.Initialize)

function netadmin.Connect() --This is what actually makes the connection.
		local config = netadmin.config --i do this alot because while I love the structuring that tables provides, I HATE the long ass syntax
		netadmin.DB = gdatabase.MakeConnection(config.databasehost, config.databaseun, config.databasepw, config.databasename, config.gdatabasesettings)
	
	if( netadmin.DB == false ) then
		netadmin.DB = 0
		return false
	else
		return true
	end
end

function netadmin.ProcessConfigs()
	Msg("NETADMIN: Loading Network Configs\n")
	file.Write("netadmin/config.txt", table.concat(netadmin.Lib.GetConfigs(), "\n"))
	ULib.execFile("netadmin/config.txt")
end

--This  is used if the Database is down and we want the server to still boot up.
function netadmin.LoadBackupConfig()
	Msg("NETADMIN: Cannot load Database!\n")
	Msg("NETADMIN: Loading BACKUP Configs\n")
	
	if file.Exists("netadmin/config.txt") then
		ULib.execFile("netadmin/config.txt")
	else
		--TODO:Put fail safe code in here!.. This will basically lock down the server completely because we have no configs
	end
end



